package com.a16.yuejian.yuejian.fragment;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.a16.yuejian.yuejian.R;
import com.a16.yuejian.yuejian.activity.PostReleaseActivity;
import com.a16.yuejian.yuejian.adapter.PostAdapter;
import com.a16.yuejian.yuejian.javabean.Post;
import com.a16.yuejian.yuejian.listener.Listener;
import com.a16.yuejian.yuejian.model.PostModel;

import java.util.List;

public class PostFragment extends Fragment {
    private RecyclerView recyclerview;
    private RecyclerView.LayoutManager mlayoutManager;
    private ImageView fabu;
    private ImageView post_img;
    private View view=null;

    Listener<Post> listener = new Listener<Post>() {
        @Override
        public void onResponse(List<Post> t) {
            recyclerview.setAdapter(new PostAdapter(getActivity(),t));
        }

        @Override
        public void onFail() {

        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view=inflater.inflate(R.layout.fragment_post, container, false);
        initRecyclerView(view);
        getData();
        initEvent();
        return view;
    }

    private void initEvent() {
        fabu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), PostReleaseActivity.class);
                getActivity().startActivity(intent);
            }
        });
    }

    private void getData() {
        PostModel postModel = new PostModel();
        postModel.allpost(listener);
    }

    private void initRecyclerView(View view) {
        recyclerview = (RecyclerView)view.findViewById(R.id.rv_post);
        mlayoutManager = new GridLayoutManager(getActivity(),2);
        recyclerview.setLayoutManager(mlayoutManager);
        fabu = (ImageView)view.findViewById(R.id.post_fabu);
        post_img = (ImageView)view.findViewById(R.id.post_img);
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }
}
